package com.hp.appfw.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.xml.sax.InputSource;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreemarkerUtil {
	public static String convert(String template, String param) throws Exception
	{
		Template temp = new Template("msgcenter", new StringReader(template), new Configuration());
		InputSource source = new InputSource(new StringReader(param));
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("param",freemarker.ext.dom.NodeModel.parse(source));
		StringWriter sw = new StringWriter();
		temp.process(root,sw);
		return sw.getBuffer().toString();
	}
	public static String loadFile(String fname) throws IOException
	{
		FileInputStream fstream = new FileInputStream(fname);
		int flength = fstream.available();
        byte b[] = new byte[flength];
        fstream.read(b);
        fstream.close();
        return new String(b);
	}
}
